import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/routers/route.dart';
import 'package:flutter_massage/generated/assets.dart';
import 'package:flutter_massage/pages/login/index.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class LoginPage extends GetView<LoginLogic> {
  const LoginPage({Key? key, this.hideBackBtn = true}) : super(key: key);

  final bool hideBackBtn;

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      titleStr: ' ',
      hideBackBtn: hideBackBtn,
      resizeToAvoidBottomInset: false,
      child: GetBuilder<LoginLogic>(
        init: LoginLogic(),
        assignId: true,
        builder: (logic) {
          return ListView(
            padding: EdgeInsets.fromLTRB(42.w, 52.w, 42.w, 0),
            keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
            children: [
              _buildLogo(),
              _buildContainer1(),
              _buildProtocol(),
            ],
          );
        },
      ),
    );
  }

  Widget _buildContainer1() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(height: 12.w),
        _buildPhoneTitle(),
        SizedBox(height: 20.w),
        _buildPhoneInput(),
        const CommonDivider(),
        SizedBox(height: 5.w),
        _buildPhoneMessage(),
        SizedBox(height: 5.w),
        _buildCodeInput(),
        const CommonDivider(),
        SizedBox(height: 5.w),
        _buildCodeMessage(),
        SizedBox(height: 50.w),
        _buildLoginBtn(),
        SizedBox(height: 5.w),
        _buildQuickLoginBtn(),
      ],
    );
  }

  Widget _buildQuickLoginBtn() {
    return ButtonWidget.text(
      '本机号码一键登录',
      height: 30.w,
      textColor: AppColors.primaryGreen,
      fontWeight: FontWeight.bold,
      fontSize: 13,
      alignment: Alignment.center,
      hasInkWell: false,
      onTap: controller.callQuickLogin,
    );
  }

  Widget _buildLogo() => Center(
        child: Container(
          decoration: CommonDecoration.getShapeDecoration(radius: 100.w),
          width: 100.w,
          height: 100.w,
          clipBehavior: Clip.antiAlias,
          child: const IconWidget.image(Assets.imageLogoDark),
        ),
      );

  Widget _buildPhoneTitle() => const Center(
        child: TextWidget.oneLine(
          text: '手机号登录 / 注册',
          color: AppColors.textLevelOne,
          fontSize: 22,
          fontWeight: FontWeight.bold,
        ),
      );

  Widget _buildPhoneInput() => TextField(
        decoration: const InputDecoration(
          border: InputBorder.none,
          hintText: '请输入手机号',
          hintStyle: TextStyle(fontSize: 15, color: AppColors.textLevelThree),
        ),
        cursorColor: AppColors.primaryGreen,
        keyboardType: TextInputType.phone,
        controller: controller.phoneController,
        focusNode: controller.phoneNode,
        inputFormatters: [LengthLimitingTextInputFormatter(11)],
      );

  Widget _buildPhoneMessage() => Obx(() => controller.showPhoneError.value
      ? const TextWidget.oneLine(
          text: '输入的手机号不正确，请重新输入',
          color: AppColors.primaryGreen,
          fontSize: 12,
        )
      : const SizedBox.shrink());

  Widget _buildCodeInput() => Row(
        children: [
          Expanded(
            child: TextField(
              decoration: const InputDecoration(
                border: InputBorder.none,
                hintText: '请输入验证码',
                hintStyle: TextStyle(fontSize: 15, color: AppColors.textLevelThree),
              ),
              cursorColor: AppColors.primaryGreen,
              keyboardType: TextInputType.phone,
              controller: controller.codeController,
              focusNode: controller.codeNode,
              inputFormatters: [LengthLimitingTextInputFormatter(6)],
            ),
          ),
          CommonCount(clickStart: controller.sendCode, startStrColor: AppColors.primaryGreen),
        ],
      );

  Widget _buildCodeMessage() => Obx(() => controller.showCodeError.value
      ? const TextWidget.oneLine(
          text: '输入的验证码不正确，请重新输入',
          color: AppColors.primaryGreen,
          fontSize: 12,
        )
      : const SizedBox.shrink());

  Widget _buildProtocol() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ButtonWidget.icon(
          Obx(() {
            return IconWidget.svg(
              controller.agreeProtocol.value ? Assets.svgCheckSelected : Assets.svgCheckNormal,
              size: 12.w,
            );
          }),
          padding: EdgeInsets.fromLTRB(0, 6.w, 6.w, 6.w),
          onTap: controller.clickAgreeProtocol,
          hasInkWell: false,
        ),
        Flexible(
          child: RichText(
            text: TextSpan(
              text: '同意',
              children: [
                TextSpan(
                  text: '《用户协议》',
                  style: const TextStyle(color: AppColors.primaryGreen),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      CommonRoute.openProtocolPage();
                    },
                ),
                const TextSpan(
                  text: '和',
                  style: TextStyle(color: AppColors.textLevelOne),
                ),
                TextSpan(
                  text: '《隐私政策》',
                  style: const TextStyle(color: AppColors.primaryGreen),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      CommonRoute.openPrivacyPage();
                    },
                ),
              ],
              style: const TextStyle(color: AppColors.textLevelOne, fontSize: 12),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildLoginBtn() => ButtonWidget.text(
        '登录',
        width: 310.w,
        height: 44.w,
        borderRadius: 44.w,
        alignment: Alignment.center,
        backgroundColor: AppColors.primaryGreen,
        textColor: AppColors.white,
        fontWeight: FontWeight.bold,
        fontSize: 16,
        onTap: controller.login,
        // hasInkWell: false,
      );
}
